
opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";

@rssi;
$recordNum = 300000;

for($i=0;$i<16;$i++){
	for($j=0;$j<$recordNum;$j++){
		$rssi[$i][$j] = -150;
	}
}

$fileNum = 0;

while ($file = readdir $dir) {
	$file_path = "$ARGV[0]\\$file";
	print ("$file_path  \n $file\n");
	
	$logfile = "\.log";
	#get input samples
	if($file =~ /$logfile/) {
		open fin, "<$file_path" or die "can not open file $file_path!\n";
		$fileNum++;
		
		$sampleNum = 0;
		$channel = 0;
		while (<fin>) {
			chomp;
			@info = split/,/;
			
			if($info[0] eq "infoc") {
				if($channel==0){
					$channel = $info[1];
				}
				next;
			}
			if($info[0] eq "infos"){
				next;
			}
			if($info[0] eq "infoe"){
				next;
			}
			
			$rssi[$channel-11][$sampleNum++] = $info[1];
		}
		close fin;
	}
	else {
		print("No log files found!\n");
		next;
	}
	
}

open fout, ">$ARGV[0]\\totalSequential.info" or die "can not open file $dir\\totalSequential.info!\n";
OUTPUT:{for($j=1;$j<$recordNum;$j++){
	for($i=0;$i<16;$i++){
		if($rssi[$i][$j]==-150){
			last OUTPUT;
		}
		print fout "$rssi[$i][$j] ";
	}
	print fout "\n";
}
}
close fout;

$busyThreshold = -85;

@totalNum;
@occpRatio;

open fout, ">$ARGV[0]\\totalSequential01.info" or die "can not open file $dir\\totalSequential01.info!\n";
OUTPUT:{for($j=1;$j<$recordNum;$j++){
	for($i=0;$i<16;$i++){
		if($rssi[$i][$j]==-150){
			next;
		}
		$totalNum[$i]++;
		if($rssi[$i][$j]>=$busyThreshold){
			print fout "1 ";
			$occpRatio[$i]++;
		}
		else {
			print fout "0 ";
		}
	}
	print fout "\n";
}
}
close fout;

open outRatio, ">$ARGV[0]\\occpRatioSequential.info" or die "can not open file $dir\\occpRatioSequential.info!\n";
for($i=0;$i<16;$i++){
	$temp = $occpRatio[$i]/$totalNum[$i];
	print outRatio "$occpRatio[$i] $totalNum[$i] $temp\n";
}
close outRatio;